using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.ComponentModel;
using MacomberMap.Common.Types;
using System.Windows.Forms;
using MacomberMap.Common.Internals;
using System.Drawing;

namespace MacomberMap.Common.User_Interfaces.Components
{
    /// <summary>
    /// The parameters for the overall display
    /// </summary>
    public class MM_Overall_Display : MM_Serializable
    {
        #region Varaible declarations
        private static int _StationNames, _LineFlows, _LineText, _StationMW, _FlowInterval = 400;
        private static float _ContourThreshold = 5f, _ContourBrightness = 400f;
        private static bool _DisplayCounties = true, _DisplayStateBorder = true, _FPS = false, _UseLongNames = true, _UseEstimates = true, _DeltaTime = false;
        private static MM_Contour_Enum _Contour = MM_Contour_Enum.None;
        
        /// <summary>Our collection of KV Levels</summary>
        public static Dictionary<String, MM_KVLevel> KVLevels = new Dictionary<string, MM_KVLevel>(10);

        /// <summary>
        /// Whether time should be reported as a delta (e.g., 1 minute ago) rather than absolute time (e.g., 12:32:12)
        /// </summary>
        [Category("Display"), Description("Whether time should be reported as a delta (e.g., 1 minute ago) rather than absolute time (e.g., 12:32:12)"), DefaultValue(true)]
        public static bool DeltaTime
        {
            get { return _DeltaTime; }
            set { _DeltaTime = value; }
        }

        /// <summary>The collection of Event images</summary>
        public static ImageList EventImages = new ImageList();

        /// <summary>The collection of Event types</summary>
        public static MM_Indexed_Dictionary<String, MM_Event_Type> EventTypes = new MM_Indexed_Dictionary<string, MM_Event_Type>(50);        

        /// <summary>The collection of element types</summary>
        public static MM_Indexed_Dictionary<String, MM_Element_Type> ElemTypes = new MM_Indexed_Dictionary<string, MM_Element_Type>(StringComparer.CurrentCultureIgnoreCase);        

        /// <summary>The collection of generation types used within the network</summary>
        public static Dictionary<String, MM_Generation_Type> GenerationTypes = new Dictionary<string, MM_Generation_Type>(10);      
                
        #endregion

        /// <summary>
        /// The collection of contours available
        /// </summary>
        public enum MM_Contour_Enum
        {
            /// <summary>Show bus voltages</summary>
            BusVoltage,
            /// <summary>No contours</summary>
            None
        }

        #region Variable interfaces
        /// <summary>
        /// The threshold (above/below mean) needed for the contour to be shown
        /// </summary>
        [Category("Contour"), Description("The threshold (above/below mean) needed for the contour to be shown"), DefaultValue(5f)]
        public static float ContourThreshold
        {
            get { return _ContourThreshold; }
            set { _ContourThreshold = value; }
        }

        /// <summary>
        /// The brightness for the contour
        /// </summary>
        [Category("Contour"), Description("The brightness for the contour"), DefaultValue(400f)]

        public static float ContourBrightness
        {
            get { return _ContourBrightness; }
            set { _ContourBrightness = value; }
        }


        /// <summary>
        /// The contour (by county) to be shown
        /// </summary>
        [Category("Contour"), Description("The contour (by county) to be shown"), DefaultValue(MM_Overall_Display.MM_Contour_Enum.None)]
        public static MM_Contour_Enum Contour
        {
            get { return _Contour; }
            set { _Contour = value; }
        }

        /// <summary>
        /// The interval at which the network flow arrows
        /// </summary>
        [Category("Display Options"), Description("The interval at which the network flow arrows"), DefaultValue(400)]
        public static int FlowInterval
        {
            get { return _FlowInterval; }
            set { _FlowInterval = value; }
        }

        /// <summary>
        /// Whether the long names should be used for substations (e.g., West Batesville vs. w_batesv)
        /// </summary>
        [Category("Display Options"), Description("Whether the number of frames per second should be displayed in the network map"), DefaultValue(true)]
        public static bool UseLongNames
        {
            get { return _UseLongNames; }
            set { _UseLongNames = value; }
        }

        /// <summary>
        /// Whether the long names should be used for substations (e.g., West Batesville vs. w_batesv)
        /// </summary>
        [Category("Display Options"), Description("Whether the estimated points should be displayed on the network map"), DefaultValue(true)]
        public static bool UseEstimates
        {
            get { return _UseEstimates; }
            set { _UseEstimates = value; }
        }

        /// <summary>
        /// Return the name of the element
        /// </summary>
        public static string Name
        {
            get { return "Overall"; }
            set { }
        }


        /// <summary>
        /// Whether the number of frames per second should be displayed in the network map
        /// </summary>
        [Category("Display Options"), Description("Whether the number of frames per second should be displayed in the network map"), DefaultValue(false)]
        public static bool FPS
        {
            get { return _FPS; }
            set { _FPS = value; }
        }

        /// <summary>
        /// Whether counties should be displayed in the network map
        /// </summary>
        [Category("Display Options"), Description("Whether counties should be displayed in the network map"), DefaultValue(true)]
        public static bool DisplayCounties
        {
            get { return _DisplayCounties; }
            set { _DisplayCounties = value; }
        }



        /// <summary>
        /// Whether counties should be displayed in the network map
        /// </summary>
        [Category("Display Options"), Description("Whether the state border should be displayed in the network map"), DefaultValue(true)]
        public static bool DisplayStateBorder
        {
            get { return _DisplayStateBorder; }
            set { _DisplayStateBorder = value; }
        }

        /// <summary>
        /// The zoom level at which the substation names become visible
        /// </summary>
        [Category("Zoom Levels"), Description("The zoom level at which the substation names become visible")]
        public static int StationNames
        {
            get { return _StationNames; }
            set { _StationNames = value; }
        }

        /// <summary>
        /// The zoom level at which the substation MWs (generation and/or load) become visible
        /// </summary>
        [Category("Zoom Levels"), Description("The zoom level at which the substation MWs (generation and/or load) become visible")]
        public static int StationMW
        {
            get { return _StationMW; }
            set { _StationMW = value; }
        }

        /// <summary>
        /// The zoom level at which the line flow graphics become visible
        /// </summary>
        [Category("Zoom Levels"), Description("The zoom level at which the line flow graphics become visible")]
        public static int LineFlows
        {
            get { return _LineFlows; }
            set { _LineFlows = value; }
        }

        /// <summary>
        /// The zoom level at which the line flow text becomes visible
        /// </summary>
        [Category("Zoom Levels"), Description("The zoom level at which the line flow text becomes visible")]
        public static int LineText
        {
            get { return _LineText; }
            set { _LineText = value; }
        }
        #endregion

        /// <summary>
        /// Locate an element type. If one is not found, add it in.
        /// </summary>
        /// <param name="ElemType"></param>
        /// <returns></returns>
        public static MM_Element_Type FindElementType(string ElemType)
        {
            MM_Element_Type OutType;
            if (!MM_Overall_Display.ElemTypes.TryGetValue(ElemType, out OutType))
                MM_Overall_Display.ElemTypes.Add(ElemType, OutType = new MM_Element_Type(ElemType, ElemType.Substring(0, 3), ""));
            return OutType;
        }

        /// <summary>
        /// Locate an KV Level. If one is not found, add it in.
        /// </summary>
        /// <param name="KVLevel">The KV level to search for</param>
        /// <returns></returns>
        public static MM_KVLevel FindKVLevel(string KVLevel)
        {

            MM_KVLevel OutLevel;
            if (MM_Overall_Display.KVLevels.TryGetValue(KVLevel, out OutLevel))
                return OutLevel;

            //Try making sure the KV tag is appropriately added            
            else if (MM_Overall_Display.KVLevels.TryGetValue(KVLevel.Replace("KV", "").Trim() + " KV", out OutLevel))
                return OutLevel;

            //Return the "Other KV" if we have it
            else if (MM_Overall_Display.KVLevels.TryGetValue("Other KV", out OutLevel))
                return OutLevel;
            else
                MM_Overall_Display.KVLevels.Add(KVLevel, OutLevel = new MM_KVLevel(KVLevel, "Green"));
            return OutLevel;
        }
    }
}
